package org.social.db.dao;

import com.sojoys.artifact.manager.DataSourceManager;
import java.sql.Connection;
import com.sojoys.artifact.build.data.base.BaseDao;
import java.util.ArrayList;
import com.sojoys.artifact.manager.JsonManager;
import org.social.db.bean.PlayerMap;
import java.sql.SQLException;
import com.sojoys.artifact.build.data.jdbc.SK_Query;
import com.google.common.collect.Lists;
import com.sojoys.artifact.tools.ToolMap;
import org.apache.commons.dbutils.handlers.MapHandler;
import com.sojoys.artifact.tools.SK_Plus;
import java.sql.ResultSet;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.dbutils.handlers.MapListHandler;
import com.sojoys.artifact.constant.CreateTableEnum;
import com.sojoys.artifact.constant.DataModelKeyEnum;
import org.apache.commons.dbutils.handlers.AbstractListHandler;
import org.apache.commons.dbutils.QueryRunner;
import com.xiaoleilu.hutool.log.Log;
import static org.social.db.StaticResource.PlayerMapResource.*;
import java.util.Set;
import com.sojoys.artifact.constant.ArtifactErrorCode;
import com.xiaoleilu.hutool.util.StrUtil;
import com.sojoys.artifact.tools.ToolError;
import org.apache.commons.dbutils.DbUtils;
import com.xiaoleilu.hutool.log.LogFactory;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.dbutils.ResultSetHandler;
import com.google.common.base.Joiner;
import static org.social.db.StaticResource.PlayerMapResource.*;
/**
 * 玩家映射表
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class PlayerMapDaoImpl implements BaseDao<Integer,PlayerMap>{
	static Log log = LogFactory.get(PlayerMapDaoImpl.class);
	
	// 代理
	
	/**
	 * 私有化无参构造方法
	 */
	private PlayerMapDaoImpl() {}
	  
    private static final PlayerMapDaoImpl INSTANCE = new PlayerMapDaoImpl();  
    
    public static PlayerMapDaoImpl me() {  
        return INSTANCE;  
    }  
	
	public PlayerMap insert(PlayerMap playerMap){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insert(playerMap,conn);
	}
	
	public PlayerMap insert(PlayerMap playerMap,Connection conn){
		return insert(playerMap,conn,TABLE_NAME);
	}
	
	public PlayerMap insert(PlayerMap playerMap,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insert(playerMap,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public PlayerMap insert(PlayerMap playerMap,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insert(playerMap,conn,tableName);
	}
	
	public PlayerMap insert(PlayerMap playerMap,Connection conn,String tableName){
		
		SK_Query sq = new SK_Query();
		String sql = "INSERT INTO " +tableName+ " (id,uid,pid,server_id,info,create_date,modified_date) VALUES (?,?,?,?,?,?,?)";
		try {
			int i = (int)sq.insert(conn,sql,playerMap.getId(),playerMap.getUid(),playerMap.getPid(),playerMap.getServerId(),playerMap.getInfoByte(),playerMap.getCreateDate(),playerMap.getModifiedDate());
			if(playerMap.getId()==0){
				playerMap.setId(i);
			}
			return i > 0 ? playerMap : null;
		} catch (Exception e) {
			log.error(e);
			return null;
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public PlayerMap insert(PlayerMap playerMap,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insert(playerMap,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public int[] insertBatch(List<PlayerMap> playerMaps){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insertBatch(playerMaps,conn);
	}
	
	public int[] insertBatch(List<PlayerMap> playerMaps,Connection conn){
		return insertBatch(playerMaps,conn,TABLE_NAME);
	}
	
	public int[] insertBatch(List<PlayerMap> playerMaps,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(playerMaps,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] insertBatch(List<PlayerMap> playerMaps,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insertBatch(playerMaps,conn,tableName);
	}
	
	public int[] insertBatch(List<PlayerMap> playerMaps,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "INSERT INTO " +tableName+ " (id,uid,pid,server_id,info,create_date,modified_date) VALUES (?,?,?,?,?,?,?)";
		try {
			int columnSize = 7;
			int size = playerMaps.size();
			Object[][] params = new Object[size][columnSize];
			for (int i = 0; i < size; i++) {
				params[i][0] =playerMaps.get(i).getId();
				params[i][1] =playerMaps.get(i).getUid();
				params[i][2] =playerMaps.get(i).getPid();
				params[i][3] =playerMaps.get(i).getServerId();
				params[i][4] =playerMaps.get(i).getInfoByte();
				params[i][5] =playerMaps.get(i).getCreateDate();
				params[i][6] =playerMaps.get(i).getModifiedDate();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0 ? is : new int[]{};
		} catch (Exception e) {
			log.error(e);
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public int[] insertBatch(List<PlayerMap> playerMaps,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(playerMaps,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public PlayerMap update(PlayerMap playerMap){
		Connection conn = DataSourceManager.me().getMainConnection();
		return update(playerMap,conn);
	}
	
	public PlayerMap update(PlayerMap playerMap,Connection conn){
		return update(playerMap,conn,TABLE_NAME);
	}
	
	public PlayerMap update(PlayerMap playerMap,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return update(playerMap,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public PlayerMap update(PlayerMap playerMap,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return update(playerMap,conn,tableName);
	}
	
	public PlayerMap update(PlayerMap playerMap,Connection conn,String tableName){
		List<PlayerMap> playerMaps = new ArrayList<PlayerMap>();
		playerMaps.add(playerMap);
		updateBatch(playerMaps,conn,tableName);
		return playerMap;
	}
	
	public PlayerMap update(PlayerMap playerMap,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return update(playerMap,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] updateBatch(List<PlayerMap> playerMaps){
		Connection conn = DataSourceManager.me().getMainConnection();
		return updateBatch(playerMaps,conn);
	}
	
	public int[] updateBatch(List<PlayerMap> playerMaps,Connection conn){
		return updateBatch(playerMaps,conn,TABLE_NAME);
	}
	
	public int[] updateBatch(List<PlayerMap> playerMaps,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(playerMaps,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] updateBatch(List<PlayerMap> playerMaps,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return updateBatch(playerMaps,conn,tableName);
	}
	
	public int[] updateBatch(List<PlayerMap> playerMaps,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "UPDATE " +tableName+ " SET id = ?,uid = ?,pid = ?,server_id = ?,info = ?,create_date = ?,modified_date = ? WHERE id = ?";
		try {
			int columnSize = 7;
			int size = playerMaps.size();
			Object[][] params = new Object[size][columnSize + 1];
			for (int i = 0; i < size; i++) {
				params[i][0] =playerMaps.get(i).getId();
				params[i][1] =playerMaps.get(i).getUid();
				params[i][2] =playerMaps.get(i).getPid();
				params[i][3] =playerMaps.get(i).getServerId();
				params[i][4] =playerMaps.get(i).getInfoByte();
				params[i][5] =playerMaps.get(i).getCreateDate();
				params[i][6] =playerMaps.get(i).getModifiedDate();
				params[i][columnSize] =playerMaps.get(i).getId();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0 ? is : new int[]{};
		} catch (Exception e) {
			log.error(e);
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public int[] updateBatch(List<PlayerMap> playerMaps,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(playerMaps,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public boolean delete(PlayerMap playerMap){
		Connection conn = DataSourceManager.me().getMainConnection();
		return delete(playerMap,conn);
	}
	
	public boolean delete(PlayerMap playerMap,Connection conn){
		return delete(playerMap,conn,TABLE_NAME);
	}
	
	public boolean delete(PlayerMap playerMap,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return delete(playerMap,conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean delete(PlayerMap playerMap,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return delete(playerMap,conn,tableName);
	}
	
	public boolean delete(PlayerMap playerMap,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE id = ?";
		try {
			int i = run.update(conn,sql, playerMap.getId());
			return i > 0 ? true : false;
		} catch (Exception e) {
			log.error(e);
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean delete(PlayerMap playerMap,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return delete(playerMap,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	public boolean deleteBatch(List<PlayerMap> playerMaps){
		Connection conn = DataSourceManager.me().getMainConnection();
		return deleteBatch(playerMaps,conn);
	}
	
	public boolean deleteBatch(List<PlayerMap> playerMaps,Connection conn){
		return deleteBatch(playerMaps,conn,TABLE_NAME);
	}
	
	public boolean deleteBatch(List<PlayerMap> playerMaps,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(playerMaps,conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean deleteBatch(List<PlayerMap> playerMaps,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return deleteBatch(playerMaps,conn,tableName);
	}
	
	public boolean deleteBatch(List<PlayerMap> playerMaps,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE id = ?";
		try {
			int size = playerMaps.size();
			Object[][] params = new Object[size][1];
			for (int i = 0; i < size; i++) {
				params[i][0] = playerMaps.get(i).getId();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0;
		} catch (Exception e) {
			log.error(e);
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean deleteBatch(List<PlayerMap> playerMaps,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(playerMaps,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}

	/**
	 * 根据( id ) 查询
	 */
	public PlayerMap getById(int id){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getById(id, conn);
	}
	
	public PlayerMap getById(int id,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getById(id, conn,tableName);
	}
	public List<PlayerMap> getIn(Set<Integer> ids){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getIn(ids, conn);
	}
	
	public List<PlayerMap> getIn(Set<Integer> ids,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getIn(ids, conn,tableName);
	}
	/**
	 * 根据( pid ) 查询
	 */
	public PlayerMap getByPid(int pid){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByPid(pid, conn);
	}
	
	public PlayerMap getByPid(int pid,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByPid(pid, conn,tableName);
	}
	/**
	 * 根据( uid ) 查询
	 */
	public PlayerMap getByUid(String uid){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByUid(uid, conn);
	}
	
	public PlayerMap getByUid(String uid,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByUid(uid, conn,tableName);
	}
	
	//Connection
	/**
	 * 根据( id ) 查询
	 */
	public PlayerMap getById(int id,Connection conn){
		return getById(id,conn,TABLE_NAME);
	}
	
	public PlayerMap getById(int id,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,uid,pid,server_id,info,create_date,modified_date FROM " + tableName + " WHERE " + "id = ? ORDER BY id ASC LIMIT 1";
		PlayerMap playerMap = null; 
		try {
			playerMap = run.query(conn,sql, new PlayerMapHandler(), id);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return playerMap;
	}
	public List<PlayerMap> getIn(Set<Integer> ids,Connection conn){
		return getIn(ids,conn,TABLE_NAME);
	}
	
	public List<PlayerMap> getIn(Set<Integer> ids,Connection conn,String tableName){
		List<PlayerMap> playerMaps = Lists.newArrayList();
		if(ids.isEmpty()) return playerMaps;
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,uid,pid,server_id,info,create_date,modified_date FROM " + tableName + " WHERE " + "id in (%s) ORDER BY id ASC";
		sql = String.format(sql, Joiner.on(",").join(ids));
		try {
			playerMaps = run.query(conn,sql, new PlayerMapListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return playerMaps;
	}
	/**
	 * 根据( pid ) 查询
	 */
	public PlayerMap getByPid(int pid,Connection conn){
		return getByPid(pid,conn,TABLE_NAME);
	}
	
	public PlayerMap getByPid(int pid,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,uid,pid,server_id,info,create_date,modified_date FROM " + tableName + " WHERE " + "pid = ? ORDER BY id ASC LIMIT 1";
		PlayerMap playerMap = null; 
		try {
			playerMap = run.query(conn,sql, new PlayerMapHandler(), pid);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return playerMap;
	}
	/**
	 * 根据( uid ) 查询
	 */
	public PlayerMap getByUid(String uid,Connection conn){
		return getByUid(uid,conn,TABLE_NAME);
	}
	
	public PlayerMap getByUid(String uid,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,uid,pid,server_id,info,create_date,modified_date FROM " + tableName + " WHERE " + "uid = ? ORDER BY id ASC LIMIT 1";
		PlayerMap playerMap = null; 
		try {
			playerMap = run.query(conn,sql, new PlayerMapHandler(), uid);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return playerMap;
	}
	
	//DataSource
	/**
	 * 根据( id ) 查询
	 */
	public PlayerMap getById(int id,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getById(id, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public PlayerMap getById(int id,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getById(id, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	public List<PlayerMap> getIn(Set<Integer> ids,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getIn(ids, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<PlayerMap> getIn(Set<Integer> ids,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getIn(ids, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	/**
	 * 根据( pid ) 查询
	 */
	public PlayerMap getByPid(int pid,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getByPid(pid, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public PlayerMap getByPid(int pid,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getByPid(pid, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	/**
	 * 根据( uid ) 查询
	 */
	public PlayerMap getByUid(String uid,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getByUid(uid, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public PlayerMap getByUid(String uid,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getByUid(uid, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	
	public List<PlayerMap> getAll(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAll(conn);
	}
	
	public List<PlayerMap> getAll(Connection conn){
		return getAll(conn,TABLE_NAME);
	}
	
	public List<PlayerMap> getAll(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<PlayerMap> getAll(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAll(conn,tableName);
	}
	
	public List<PlayerMap> getAll(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,uid,pid,server_id,info,create_date,modified_date FROM " + tableName + " ORDER BY id ASC";
		List<PlayerMap> playerMaps = Lists.newArrayList();
		try {
			playerMaps = run.query(conn, sql, new PlayerMapListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return playerMaps;
	}
	
	public List<PlayerMap> getAll(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public List<PlayerMap> getAllPage(int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAllPage(conn,page,pageSize);
	}
	
	public List<PlayerMap> getAllPage(Connection conn,int page,int pageSize){
		return getAllPage(conn,TABLE_NAME,page,pageSize);
	}
	
	public List<PlayerMap> getAllPage(DataSource ds,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<PlayerMap> getAllPage(String tableName,int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAllPage(conn,tableName,page,pageSize);
	}
	
	public List<PlayerMap> getAllPage(Connection conn,String tableName,int page,int pageSize){
		QueryRunner run = new QueryRunner();
		page = ((page-1) * pageSize);
		String sql = "SELECT id,uid,pid,server_id,info,create_date,modified_date FROM " + tableName + " ORDER BY id ASC LIMIT " + page + " , " +pageSize;
		List<PlayerMap> playerMaps = Lists.newArrayList();
		try {
			playerMaps = run.query(conn, sql, new PlayerMapListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return playerMaps;
	}
	
	public List<PlayerMap> getAllPage(DataSource ds,String tableName,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,tableName,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public boolean truncate(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return truncate(conn);
	}
	
	public boolean truncate(Connection conn){
		return truncate(conn,TABLE_NAME);
	}
	
	public boolean truncate(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return truncate(conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean truncate(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return truncate(conn,tableName);
	}
	
	public boolean truncate(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "TRUNCATE " + tableName;
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean truncate(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return truncate(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	// 执行自定义查询sql
	public List<Map<String,Object>> executeQuerySql(String sql){
		Connection conn = DataSourceManager.me().getMainConnection();
		return executeQuerySql(conn,sql);
	}
	
	public List<Map<String,Object>> executeQuerySql(Connection conn,String sql){
		return executeQuerySql(conn,TABLE_NAME,sql);
	}
	
	public List<Map<String,Object>> executeQuerySql(DataSource ds,String sql){
		try {
			Connection conn = ds.getConnection();
			return executeQuerySql(conn,sql);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Map<String,Object>> executeQuerySql(String tableName,String sql){
		Connection conn = DataSourceManager.me().getMainConnection();
		return executeQuerySql(conn,tableName);
	}
	
	public List<Map<String,Object>> executeQuerySql(Connection conn,String tableName,String sql){
		QueryRunner run = new QueryRunner();
		ToolError.isAndTrue(ArtifactErrorCode.SQL_PERMISSIONS_ERROR, !StrUtil.startWithIgnoreCase(sql, "SELECT"));
		List<Map<String,Object>> list = Lists.newArrayList();
		try {
			list = run.query(conn,sql, new MapListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return list;
	}
	/**
	public List<Map<String,Object>> executeQuerySql(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return executeQuerySql(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	*/
	
	//
	
	public int getLastId(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getLastId(conn);
	}
	
	public int getLastId(Connection conn){
		return getLastId(conn,TABLE_NAME);
	}
	
	public int getLastId(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getLastId(conn);
		} catch (Exception e) {
			log.error(e);
			return 0;
		}
	}
	
	public int getLastId(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getLastId(conn,tableName);
	}
	
	public int getLastId(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id FROM " + tableName + " ORDER BY id DESC LIMIT 1";
		try {
			Map result = run.query(conn, sql, new MapHandler());
			return result == null ? 0 : ToolMap.getInt("id",result,0);
		} catch (Exception e) {
			log.error(e);
			return 0;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return 0;
			}
		}
	}
	
	public int getLastId(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getLastId(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return 0;
		}
	}
	//
	
	//Drop Table
	public boolean drop(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return drop(conn);
	}
	
	public boolean drop(Connection conn){
		return drop(conn,TABLE_NAME);
	}
	
	public boolean drop(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return drop(conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean drop(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return drop(conn,tableName);
	}
	
	public boolean drop(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DROP TABLE " + tableName;
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean drop(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return drop(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	//create
	public boolean createTable(CreateTableEnum type){
		Connection conn = DataSourceManager.me().getMainConnection();
		return createTable(conn,type);
	}
	
	public boolean createTable(Connection conn,CreateTableEnum type){
		return createTable(conn,TABLE_NAME,type);
	}
	
	public boolean createTable(DataSource ds,CreateTableEnum type){
		try {
			Connection conn = ds.getConnection();
			return createTable(conn,type);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean createTable(String tableName,CreateTableEnum type){
		Connection conn = DataSourceManager.me().getMainConnection();
		return createTable(conn,tableName,type);
	}
	
	public boolean createTable(Connection conn,String tableName,CreateTableEnum type){
		QueryRunner run = new QueryRunner();
		SK_Plus plus = SK_Plus.b("CREATE TABLE IF NOT EXISTS `", tableName,"` (");
		plus.a("  `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '',");	
		plus.a("  `uid` VARCHAR(45)   COMMENT 'remarks=账号库ID',");	
		plus.a("  `pid` INT(11)   COMMENT 'remarks=玩家ID',");	
		plus.a("  `server_id` VARCHAR(45)   COMMENT 'remarks=服务器ID',");	
		plus.a("  `info` BLOB(65535)   COMMENT 'remarks=玩家信息',");	
		plus.a("  `create_date` DATETIME   COMMENT '',");
		plus.a("  `modified_date` DATETIME   COMMENT '',");
		plus.a("  PRIMARY KEY (`id`),");
		if (CreateTableEnum.LOGIC.equals(type) || CreateTableEnum.DESIGN.equals(type)) {
			plus.a("  UNIQUE KEY `player_id_que` (`pid`),");
			plus.a("  UNIQUE KEY `uid_que` (`uid`),");
			
		}
		if (CreateTableEnum.DESIGN.equals(type)) {
		}
		String sql = plus.e();
		sql = StrUtil.subPre(sql, sql.lastIndexOf(StrUtil.C_COMMA));
		sql+=") ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 COMMENT='remarks=玩家映射表';";
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean createTable(DataSource ds,String tableName,CreateTableEnum type){
		try {
			Connection conn = ds.getConnection();
			return createTable(conn,tableName,type);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	public String getTableName(){
		return TABLE_NAME;
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> getColumnsInfo(){
		SK_Plus sp = SK_Plus.newSK_Plus();
		sp.a("[");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"NO\",\"TABLE_NAME\":\"player_map\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"id\",\"NULLABLE\":0,\"REMARKS\":\"\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"YES\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":1,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"player_map\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"uid\",\"NULLABLE\":1,\"REMARKS\":\"remarks=账号库ID\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":135,\"ORDINAL_POSITION\":2,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":12,\"TYPE_NAME\":\"VARCHAR\",\"COLUMN_SIZE\":45}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"player_map\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"pid\",\"NULLABLE\":1,\"REMARKS\":\"remarks=玩家ID\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":3,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"player_map\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"server_id\",\"NULLABLE\":1,\"REMARKS\":\"remarks=服务器ID\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":135,\"ORDINAL_POSITION\":4,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":12,\"TYPE_NAME\":\"VARCHAR\",\"COLUMN_SIZE\":45}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"player_map\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"info\",\"NULLABLE\":1,\"REMARKS\":\"remarks=玩家信息\ntype=Map\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":65535,\"ORDINAL_POSITION\":5,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":-4,\"TYPE_NAME\":\"BLOB\",\"COLUMN_SIZE\":65535}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"player_map\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"create_date\",\"NULLABLE\":1,\"REMARKS\":\"\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":6,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":93,\"TYPE_NAME\":\"DATETIME\",\"COLUMN_SIZE\":19}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"player_map\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"modified_date\",\"NULLABLE\":1,\"REMARKS\":\"\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":7,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":93,\"TYPE_NAME\":\"DATETIME\",\"COLUMN_SIZE\":19}").a(",");
        sp.a("]");
        return JsonManager.me().getJson().parse(sp.e(), List.class);
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> getIndexsInfo(){
		SK_Plus sp = SK_Plus.newSK_Plus();
		sp.a("[");
       	sp.a("{\"NON_UNIQUE\":false,\"TABLE_CAT\":\"dal_social_design\",\"PAGES\":null,\"TABLE_NAME\":\"player_map\",\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"id\",\"INDEX_NAME\":\"PRIMARY\",\"ASC_OR_DESC\":\"A\",\"INDEX_QUALIFIER\":\"dal_social_design\",\"ORDINAL_POSITION\":1,\"CARDINALITY\":0,\"FILTER_CONDITION\":null,\"TYPE\":3}").a(",");
       	sp.a("{\"NON_UNIQUE\":false,\"TABLE_CAT\":\"dal_social_design\",\"PAGES\":null,\"TABLE_NAME\":\"player_map\",\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"pid\",\"INDEX_NAME\":\"player_id_que\",\"ASC_OR_DESC\":\"A\",\"INDEX_QUALIFIER\":\"dal_social_design\",\"ORDINAL_POSITION\":1,\"CARDINALITY\":0,\"FILTER_CONDITION\":null,\"TYPE\":3}").a(",");
       	sp.a("{\"NON_UNIQUE\":false,\"TABLE_CAT\":\"dal_social_design\",\"PAGES\":null,\"TABLE_NAME\":\"player_map\",\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"uid\",\"INDEX_NAME\":\"uid_que\",\"ASC_OR_DESC\":\"A\",\"INDEX_QUALIFIER\":\"dal_social_design\",\"ORDINAL_POSITION\":1,\"CARDINALITY\":0,\"FILTER_CONDITION\":null,\"TYPE\":3}").a(",");
        sp.a("]");
        return JsonManager.me().getJson().parse(sp.e(), List.class);
	}
//自定义内容起始位置
class PlayerMapHandler implements ResultSetHandler<PlayerMap>{
	@Override
	public PlayerMap handle(ResultSet rs) throws SQLException {
		return rs.next() ? PlayerMap.builder().createForResultSet(rs).syncIndexValues() : null ;
	}
}

class PlayerMapListHandler extends AbstractListHandler<PlayerMap>{
	@Override
	protected PlayerMap handleRow(ResultSet rs) throws SQLException {
		return PlayerMap.builder().createForResultSet(rs).syncIndexValues();
	}
}

	public PlayerMap getByLastByPid(){
		Connection conn = DataSourceManager.me().getMainConnection();
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,uid,pid,server_id,info,create_date,modified_date FROM " + TABLE_NAME + " ORDER BY pid DESC LIMIT 1";
		PlayerMap playerMap = null;
		try {
			playerMap = run.query(conn,sql, new PlayerMapHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return playerMap;
	}

//自定义内容结束位置
}

